package com.lz.demo.log.log4j2.mongodb;

import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bson.Document;

/**
 * 日志级别从高到低：fatal、error、warn、info、debug、trace
 * 另外，还有 off 和 all 表示关闭和记录所有日志
 */
public class Log4j2Write2MongodbTest {

    static final Logger LOGGER = LogManager.getLogger(Log4j2Write2MongodbTest.class);

    public static void main( String[] args ) {
        test1();
//        test2();
    }

    /**
     * 测试直连数据库读取
     */
    public static void test2() {
        MongoClient mongoClient = MongoClients.create("mongodb://root:root@192.168.101.210:27017/test?authSource=admin");
        MongoDatabase database = mongoClient.getDatabase("test");
        MongoCollection<Document> collection = database.getCollection("testTable");
        Document first = collection.find().first();
        System.out.println(first);
    }

    /**
     * 测试 log 写入数据库
     */
    public static void test1(){
        LOGGER.debug("log4j2 debug print");
        LOGGER.info("log4j2 info print");
        LOGGER.warn("log4j2 warn print");
        LOGGER.error("log4j2 error print");
        LOGGER.fatal("log4j2 fatal print");
    }
}
